package ar.com.raulsanchez.syncrolist;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class WebServiceInterface {

	private static final String NAMESPACE 				= "xsd:syncrolist";
	private static final String URL 					= "http://www.raulsanchez.com.ar/syncrolist/webservice.php";
	private static final String ACTION_LOGIN 			= "login";
	private static final String ACTION_REGISTER 		= "register";
	private static final String ACTION_ADD_ACTIVITY		= "addActivity";
	private static final String ACTION_DEL_ACTIVITY 	= "deleteActivity";
	private static final String ACTION_UPD_ACTIVITY 	= "updateActivity";
	private static final String ACTION_LIST_ACTIVITIES  = "listActivities";

	// LOGIN
	public int login(String username, String password) {
				
    	SoapObject request = new SoapObject(NAMESPACE, ACTION_LOGIN);
    	request.addProperty("username", username);
 		request.addProperty("password", password);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(URL + "/" + ACTION_LOGIN, envelope);
            SoapObject response = (SoapObject)envelope.bodyIn;
            return Integer.parseInt(response.getProperty(0).toString());
        }
        catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
	} // login
	
	// REGISTER
	public int register(String username, String hashedPassword) {
	   	SoapObject request = new SoapObject(NAMESPACE, ACTION_REGISTER);
    	request.addProperty("username", username);
 		request.addProperty("password", hashedPassword);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(URL + "/" + ACTION_REGISTER, envelope);
            SoapObject response = (SoapObject)envelope.bodyIn;
            return Integer.parseInt(response.getProperty(0).toString());
        }
        catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
	} // register
	
	// LIST_ACTIVITIES
	public String listActivities(Integer usuario_id) {
		
    	SoapObject request = new SoapObject(NAMESPACE, ACTION_LIST_ACTIVITIES);
    	request.addProperty("usuario_id", usuario_id);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

      
        try {
			androidHttpTransport.call(URL + "/" + ACTION_LIST_ACTIVITIES, envelope);
			SoapObject response = (SoapObject)envelope.bodyIn;
            return response.getProperty(0).toString();
		} catch (IOException e) {
			Log.e("Error : " , e.getMessage());
			e.printStackTrace();
			return "0";
		} catch (XmlPullParserException e) {
			Log.e("Error : " , e.getMessage());
			e.printStackTrace();
			return "0";
		} catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return "0";
        }
        
	} // listActivities
	
	// ADD_ACTIVITY
	public int addActivity(int usuario_id, String titulo, String detalle, String fecha, int version) {
		
    	SoapObject request = new SoapObject(NAMESPACE, ACTION_ADD_ACTIVITY);
    	request.addProperty("usuario_id", usuario_id);
    	request.addProperty("titulo", titulo);
    	request.addProperty("detalle", detalle);
    	request.addProperty("fecha", fecha);
    	request.addProperty("version", version);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(URL + "/" + ACTION_ADD_ACTIVITY, envelope);
            SoapObject response = (SoapObject)envelope.bodyIn;
            return Integer.parseInt(response.getProperty(0).toString());
        }
        catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
	} // addActivity
	
	// DEL_ACTIVITY
	public int delActivity(int actividad_id) {
		
    	SoapObject request = new SoapObject(NAMESPACE, ACTION_DEL_ACTIVITY);
    	request.addProperty("actividad_id", actividad_id);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(URL + "/" + ACTION_DEL_ACTIVITY, envelope);
            SoapObject response = (SoapObject)envelope.bodyIn;
            return Integer.parseInt(response.getProperty(0).toString());
        }
        catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
	} // delActivity
	
	// UPD_ACTIVITY
	public int updActivity(int actividad_id, String titulo, String detalle, String fecha, int version) {
		
    	SoapObject request = new SoapObject(NAMESPACE, ACTION_UPD_ACTIVITY);
    	request.addProperty("actividad_id", actividad_id);
    	request.addProperty("titulo", titulo);
    	request.addProperty("detalle", detalle);
    	request.addProperty("fecha", fecha);
    	request.addProperty("version", version);
 		
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(URL + "/" + ACTION_UPD_ACTIVITY, envelope);
            SoapObject response = (SoapObject)envelope.bodyIn;
            return Integer.parseInt(response.getProperty(0).toString());
        }
        catch (Exception e) {
        	Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
	} // updActivity
	 
} // class WebServiceInterface
